package com.easygem.front.service.imp;

import com.easygem.common.model.front.TaskPackage;
import com.easygem.common.model.system.User;
import com.easygem.common.vo.front.TaskPackageVO;
import com.easygem.common.vo.system.PageVO;
import com.easygem.front.mapper.DepartmentMapper;
import com.easygem.front.mapper.ResourceInfoMapper;
import com.easygem.front.mapper.TaskPackageMapper;
import com.easygem.front.service.TaskPackageService;
import com.easygem.system.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 李鹏志
 * @version 1.0
 * @Description TODO
 * @date 2024/12/23 16:30
 */
@Service
public class TaskPackageServiceImpl implements TaskPackageService {
    @Autowired
    private TaskPackageMapper taskPackageMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private ResourceInfoMapper resourceInfoService;
    /**
     * 根据条件查询任务包
     *
     * @param taskPackageVO
     * @return 任务包列表
     */
    @Override
    public PageVO<TaskPackage> getAllTaskPackage(Integer pageNum, Integer pageSize, TaskPackageVO taskPackageVO) {
        //获取当前登录用户对象
//        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
//        if (activeUser.getUser().getType()!=2){
//            List<Long> ids= DepUtils.getDepartmentAndChildrenIds(departmentMapper.selectAll(),activeUser.getUser().getDeptId());
//            taskPackageVO.setIds(ids);
//        }else {
//            taskPackageVO.setUserId(activeUser.getUser().getUserId());
//        }
        PageHelper.startPage(pageNum,pageSize);
        TaskPackage taskPackage = new TaskPackage();
        //查询条件
        BeanUtils.copyProperties(taskPackageVO,taskPackage);
        //存入当前登录用户的账户名
//        taskPackage.setAccountName(activeUser.getUser().getAccountName());
        //查询所有任务包
        List<TaskPackage> taskPackageList = taskPackageMapper.getAllTaskPackage(taskPackage);

        PageInfo<TaskPackage> info = new PageInfo<>(taskPackageList);

        return new PageVO<>(info.getTotal(),info.getList());
    }

    /**
     * 根据id查询任务包
     *
     * @param id 任务包id
     * @return 任务包
     */
    @Override
    public TaskPackage getTaskPackageById(Long id) {
        return taskPackageMapper.getTaskPackageById(id);
    }

    /**
     * 根据任务包id删除任务包
     * @param taskPackageId 任务包id
     * @return 删除的数量
     */
    @Transactional
    public int delByTaskPackageId(Integer taskPackageId){
        return taskPackageMapper.delByTPResourceId(taskPackageId);
    }

    /**
     * 删除所有任务包创建人状态=-2的任务包数据
     *
     * @return 删除的数量
     */
    @Override
    public int delTPResourceByUserStatus() {
        return taskPackageMapper.delTPResourceByUserStatus();
    }

    /**
     * 根据任务包名查询任务包id
     *
     * @param taskPackageName 任务包编号
     * @return 任务包id
     */
    @Override
    public Integer getTaskPackageIdByName(String taskPackageName) {
        return taskPackageMapper.getTaskPackageIdByName(taskPackageName);
    }

    /**
     * 更新任务包
     *
     * @param taskPackage
     * @return
     */
    @Transactional
    @Override
    public int updateTaskPackage(TaskPackage taskPackage) {
        User user = userService.selectUserById(taskPackage.getUserId());
        resourceInfoService.updateResourcesAccountNameByTackId(taskPackage.getTaskPackageId(),user.getAccountName());
        return taskPackageMapper.updateTaskPackage(taskPackage);
    }

}
